package com.mooc.lib_user.ui.login

import android.content.Intent
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.mooc.lib_base_application.BaseApplication
import com.mooc.lib_common.action.BusAction
import com.mooc.lib_common.action.BusConst
import com.mooc.lib_common.mvvm.BaseActivity
import com.mooc.lib_user.R
import com.mooc.lib_user.databinding.ActivityLayoutLoginBinding
import com.tencent.connect.UserInfo
import com.tencent.connect.common.Constants
import com.tencent.tauth.IUiListener
import com.tencent.tauth.Tencent
import com.tencent.tauth.UiError
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

@Route(path = "/lib_user/LoginActivity")
class LoginActivity : BaseActivity<LoginViewModel, ActivityLayoutLoginBinding>(),
    View.OnClickListener, IUiListener {

    var tencent: Tencent ?= null

    override fun initView() {
        v.actionClose.setOnClickListener(this)
        v.actionLogin.setOnClickListener(this)
    }

    override fun bindLayout(): Int {
        return R.layout.activity_layout_login
    }

    override fun initData() {
        if (tencent == null)
            tencent = Tencent.createInstance("101794421", BaseApplication.instance)
        vm.loginBus.observe(this,{
            finish()
            EventBus.getDefault().post(BusAction(BusConst.LOGINACTION,null))
        })

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == Constants.REQUEST_LOGIN) {
            Tencent.onActivityResultData(requestCode, resultCode, data, this)
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onClick(v: View?) {
        if (R.id.action_close == v!!.id){
            finish()
        }else if (R.id.action_login == v!!.id){
//            if (!tencent!!.isSessionValid()){
            tencent!!.login(this, "all", this)
//            }
        }
    }

    //    var flag = true
    var openid: String ?=null
    var expires_time: Long = 0
    override fun onComplete(p0: Any?) {
        Log.e("ZXY", "onComplete:")
        //区分获取token或者使用token获取用户信息
        val response = p0 as JSONObject
        val iterator = response.keys()
        while (iterator.hasNext()){
            if (iterator!!.next()!!.equals("openid")){
                //授权返回
                openid = response.getString("openid")
                val access_token: String = response.getString("access_token")
                val expires_in: String = response.getString("expires_in")
                expires_time = response.getLong("expires_time")
                tencent!!.setAccessToken(access_token,expires_in)
                tencent!!.openId = openid
                val info = UserInfo(this, tencent!!.qqToken)
                info.getUserInfo(this)
                return
            }
        }
        val avatar : String = response.getString("figureurl")
        val name : String = response.getString("nickname")
        val description : String = response.getString("constellation")
        vm.login(avatar,description,expires_time,name,openid!!)
    }

    override fun onError(p0: UiError?) {
        Log.e("ZXY", "onError:")
        ToastUtils.showShort(p0!!.errorMessage)
    }

    override fun onCancel() {
        Log.e("ZXY", "onCancel:")
        ToastUtils.showShort("取消授权")
    }

}